package com.spc.car.data.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.collect.ImmutableMap;
import com.spc.car.data.domain.project.vo.ProjectMemberVO;
import com.spc.car.data.domain.project.vo.UserForAssignVO;
import com.spc.car.data.entity.project.ProjectTeam;
import com.spc.car.data.mapper.project.ProjectTeamMapper;
import com.spc.car.data.service.project.ProjectTeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * <p>
 * 项目团队 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-03-31
 */
@Service
public class ProjectTeamServiceImpl extends ServiceImpl<ProjectTeamMapper, ProjectTeam> implements ProjectTeamService {

    @Override
    public int removeMember(String code,String userId) {

        Map<String , Object> map = new HashMap<>();
        map.put("project_tendering_code" , code);
        map.put("user_account" , userId);
        return baseMapper.deleteByMap(map);
    }


    @Override
    public int removeProjectTeam(String code) {
        LambdaQueryWrapper<ProjectTeam> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ProjectTeam::getProjectTenderingCode , code);
        return baseMapper.delete(lambdaQueryWrapper);
    }


    @Override
    public int updateProjectTeam(ProjectTeam projectTeam){
        UpdateWrapper<ProjectTeam> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("project_tendering_code" , projectTeam.getProjectTenderingCode())
                .eq("user_account" , projectTeam.getUserAccount());

        ProjectTeam setProjectTeam = new ProjectTeam();
        setProjectTeam.setRemark(projectTeam.getRemark());

        return  baseMapper.update(setProjectTeam , updateWrapper);

    }


    @Override
    public List<String> listAssignee(@NotBlank String projectTenderingCode, @NotBlank String taskKey) {
        List<ProjectTeam> projectTeams = listByMap(ImmutableMap.of("project_tendering_code", projectTenderingCode));
        List<String> result = new ArrayList<>();
        /*if (CollectionUtils.isNotEmpty(projectTeams)) {
            result = projectTeams.stream().filter( member -> {
                if (StringUtils.isNotBlank(member.getUserTasks())) {
                    List<String> taskKeys = Arrays.asList(member.getUserTasks().split(","));
                    return taskKeys.contains(taskKey);
                }
                return false;
            }).map(ProjectTeam::getUserAccount).collect(Collectors.toList());
        }*/
        return result;
    }

    @Override
    public List<UserForAssignVO> listUserForAssign(@NotNull Integer deptId) {

        return baseMapper.listUserForAssign(deptId);
    }

    @Override
    public List<ProjectMemberVO> listMember(String projectTenderingCode) {
        return baseMapper.listMember(projectTenderingCode);
    }

    @Override
    public List<ProjectTeam> listByProjectCode(@NotBlank String projectTenderingCode) {

        return list(new QueryWrapper<ProjectTeam>()
                .eq("project_tendering_code", projectTenderingCode));
    }

    @Override
    public List<ProjectTeam> listByProjectCode(@NotBlank String projectTenderingCode,
                                               List<Integer> projectRoleIds) {

        return list(new QueryWrapper<ProjectTeam>()
                .eq("project_tendering_code", projectTenderingCode)
                .in(CollectionUtils.isNotEmpty(projectRoleIds),
                        "project_role_id", projectRoleIds)
        );
    }

}
